var show_ad_cookie_name = 'show_ad_cookie_name';
var shield_ad_config = {
	"expires" : 25,
	"no" : 3
};
$(function() {
	$("#ad_shield").click(function(event) {
		set_shield_ad();
	});
	$("#ad_shield").attr('href', '#').attr('target', '_self');
	if (is_show_ad()) {
		$("#ad_shield").html('屏蔽漂浮广告');
	} else {
		$("#ad_shield").html('广告已屏蔽');
	}
})

function StringToDate(DateStr) {
	if (typeof DateStr == "undefined")
		return new Date();
	if (typeof DateStr == "date")
		return DateStr;
	var converted = Date.parse(DateStr);
	var myDate = new Date(converted);
	if (isNaN(myDate)) {
		DateStr = DateStr.replace(/:/g, "-");
		DateStr = DateStr.replace(" ", "-");
		DateStr = DateStr.replace(".", "-");
		var arys = DateStr.split('-');
		switch (arys.length) {
		case 7:
			myDate = new Date(arys[0], --arys[1], arys[2], arys[3], arys[4],
					arys[5], arys[6]);
			break;
		case 6:
			myDate = new Date(arys[0], --arys[1], arys[2], arys[3], arys[4],
					arys[5]);
			break;
		default:
			myDate = new Date(arys[0], --arys[1], arys[2]);
			break;
		}
		;
	}
	;
	return myDate;
}

function is_show_ad() {
	var cookie = $.cookie(show_ad_cookie_name);
	if (cookie) {
		cookie = cookie.split('|');
		var no = parseInt(cookie[0]);

		var expires = StringToDate(cookie[1]);
		var date = new Date();
		if (expires > date && no <= shield_ad_config.no) {
			return false;
		}
	}
	return true;
}

function set_shield_ad() {
	if (is_show_ad()) {
		var date = new Date();
		date.setDate(date.getDate() + 1);
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		var cookie = $.cookie(show_ad_cookie_name);
		var no = 1;
		if (cookie) {
			cookie = cookie.split('|');
			no = parseInt(cookie[0]) + 1;
		}
		if (no <= shield_ad_config.no) {
			var now = new Date();
			now.setMinutes(now.getMinutes() + shield_ad_config.expires);
			cookie = no + '|' + now.getFullYear() + '-' + (now.getMonth() + 1)
					+ '-' + now.getDate() + ' ' + now.getHours() + ':'
					+ now.getMinutes() + ':' + now.getSeconds();
			$.cookie(show_ad_cookie_name, cookie, {
				expires : date
			});
			alert('广告已经屏蔽');
			window.location.reload();
		}
	}
}

function video_statistics() {
	return ''
	$.getJSON('/api.php?op=video_statistics', '', function(json) {
		$("#video_statistics").html(
				'本站共有 <strong style="color: #f44434">' + json.count
						+ '</strong> 部动漫，今日更新 <strong style="color: #f44434">'
						+ json.daycount + '</strong> 部动漫')
	});
}
function desktop() {
	return window.open('/desktop.php?url='
			+ encodeURIComponent(window.location.href) + '&name='
			+ encodeURIComponent(document.title));
}
/* 观看记录 */
function display() {
	document.getElementById("record_box").style.display = "block";
	document.getElementById("record_btn").className = "record arrowBottom";
}

function disappear() {
	document.getElementById("record_box").style.display = "none";
	document.getElementById("record_btn").className = "record arrowUp";
}

function record() {
	return;
	var url = "/api.php?op=record&is_r=1";
	$
			.getJSON(
					url,
					'',
					function(json) {
						var html = '';
						$.each(json, function(index, val) {
							html += '<li><a href="' + val.url + '">'
									+ val.title + '<em>更新至' + val.v_state
									+ '集</em></a></li>';
						});
						if (html == "") {
							$("#record_box .close")
									.html(
											'<a href="javascript:void(0);" class="red">暂无记录</a>');
						} else {
							$("#record_box .recordList").html(html);
							$("#record_box .close")
									.html(
											'<a href="javascript:void(0);" class="red" onclick="record_clear()">清空浏览记录</a>');
						}
					});
}

function changeorder(e, no) {
	$(e).parents('span.r').find('a').removeClass('none');
	$(e).addClass('none');
	$(e).parents('div.box01').find('ul li').each(function() {
		$(this).prependTo($(this).parent())
	});
	return false;
}

function record_clear() {
	var url = "/api.php?op=record&clear=1";
	$.getJSON(url, '', function(json) {
		$("#record_box .close").html(
				'<a href="javascript:void(0);" class="red">暂无记录</a>');
		$("#record_box .recordList").html('');
	});
}

function gengxin(id) {
	if ($("#update_days").attr('data-catid') == '7') {
		var url = "/api.php?op=gengxin&id=" + id;
		$
				.getJSON(
						url,
						'',
						function(json) {
							if (json.s == "y") {
								var html = '<div class="gengxin l mt10"><div class="gengxin_btn l"><a href="http://qzs.qq.com/snsapp/app/bee/widget/open.htm#content='
										+ encodeURIComponent(json.title)
										+ '[第'
										+ encodeURIComponent(json.state)
										+ '集] '
										+ '&time='
										+ encodeURIComponent(json.date)
										+ '&advance=5&url='
										+ encodeURIComponent(json.url)
										+ '" target="_blank"><img src="http://i.gtimg.cn/snsapp/app/bee/widget/img/1_1.png" style="border:0px;"/></a></div><p>'
										+ json.title
										+ ' 第'
										+ json.state
										+ '集 预计 '
										+ json.strdate
										+ ' 左右更新，敬请关注！</p></div>';
								$('#update_days').html(html);
							}
						});
	}
}

function get_score(id) {
	var url = "/api.php?op=score&id=" + id;
	$.getJSON(url, '', function(json) {
		if (json.s == "y") {
			set_score_html(json.info, id);
		}
	});
}

function set_score(id, score) {
	var url = "/api.php?op=score&score=" + score + "&id=" + id;
	$.getJSON(url, '', function(json) {
		if (json.s == "y") {
			set_score_html(json.info, id);
		} else {
			alert("暂时无法评分");
		}
	});
}

function set_score_html(info, id) {
	$(".score_num").html(info.s);
	$(".score_total").html('分 <span>/</span> ' + info.l + '人参与评分');
	var width = 0;
	for (var i = 2; i <= 10; i = i + 2) {
		$("#score_list_s" + i + ' em').html(info['s' + i]);
		width = info.l == 0 ? 0 : 100 * parseInt(info['s' + i] / info.l);
		$("#score_list_s" + i + ' i').attr('style', 'width:' + width + 'px;');
	}

	if (info.user == 0) {
		$("#star li a").bind('click', function(event) {
			set_score(id, $(this).attr('title'));
		});
	} else {
		$("#star li a").removeClass('hover').unbind('click');
		$("#star li:eq(" + (parseInt(info.user / 2, 10) - 1) + ") a").addClass(
				'hover');
	}
}

function list_tab(flag) {
	if (flag == 'hot') {
		$('#tab_pub').removeClass();
		$("#tab_hot").attr("class", "active");

		$('#cont_pub').hide();
		$('#cont_hot').show();
	} else if (flag == 'pub') {
		$('#tab_hot').removeClass();
		$("#tab_pub").attr("class", "active");

		$('#cont_hot').hide();
		$('#cont_pub').show();
	}
}

function addFavorite(url, title) {
	if (document.all) {// IE
		try {
			window.external.addFavorite(url, title);
		} catch (e) {
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
	} else {
		try {
			window.external.addFavorite(url, title);
		} catch (e) {
			try {
				window.sidebar.addPanel(title, url, '');
			} catch (e) {
				alert("请按 Ctrl+D 键添加到收藏夹");
			}
		}
	}
}

function xinfan_tab(n) {
	$('.xinfan-nav li').removeClass('on');
	$('.xinfan-nav li:eq(' + n + ')').addClass('on');
	$("#xinfan_tab_content .tab_content").hide();
	$("#xinfan_tab_content .tab_content:eq(" + n + ")").show();
	$("#xinfan_tab_content .tab_content:eq(" + n + ") .xfbox_c .current img")
			.each(function() {
				original($(this))
			});
}

function is_show_list() {
	var str = $('#like_list1').html();
	if (str.length < 20) {
		$('#like_list1').hide();
		$('#like_list2').show();
	}
}

function switch_tab(flag) {
	if (flag == 'rebo') {
		$('#menu_lianzai').removeClass();
		$('#menu_rebo').attr("class", "active");

		$('#index_tab_rebo').show();
		$('#index_tab_lianzai').hide();
	}

	if (flag == 'lianzai') {
		$('#menu_rebo').removeClass();
		$('#menu_lianzai').attr("class", "active");

		$('#index_tab_lianzai').show();
		$('#index_tab_rebo').hide();
	}
}

/* 播放列表升降序 */
function sortBtn() {
	function $$(id) {
		return document.getElementById(id);
	}
	;
	var ulCont = $$("mun_ma");
	if (ulCont) {
		var oAsc = $$("J-asc");
		var oDesc = $$("J-desc");
		var liLists = ulCont.getElementsByTagName("li");
		var ulDesc = document.createElement("ul");
		var ulAsc = document.createElement("ul");
		var len = liLists.length;
		for (var i = 0; i < len; i++) {
			var liTemp = liLists[i].cloneNode(1);
			var liTemp2 = liLists[len - i - 1].cloneNode(1)
			ulAsc.appendChild(liTemp);
			ulDesc.appendChild(liTemp2);
		}
		;
		oAsc.onclick = function() {
			if (!this.className.indexOf("cur") > -1) {
				this.className += " asces_cur";
				oDesc.className = oDesc.className.replace(/desces_cur/g, "");
				ulCont.innerHTML = ulAsc.innerHTML;
			}
			;
			return false;
		};
		oDesc.onclick = function() {
			if (!this.className.indexOf("cur") > -1) {
				this.className += " desces_cur";
				oAsc.className = oAsc.className.replace(/asces_cur/g, "");
				ulCont.innerHTML = ulDesc.innerHTML;
			}
			;
			return false;
		};
	}
	;
};

function Trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
};

// 节目单
function playbill() {
	var now = new Date();
	var w = now.getDay();
	$("#tabweek li:eq(" + w + ")").removeClass('normal').addClass('current');
	$(".weekcon div:eq(" + w + ")").removeClass('normal').addClass('current');
}

/* cookie对象 */
cookie = {
	get : function(name) {
		var cookieStr = "; " + document.cookie + "; ";
		var index = cookieStr.indexOf("; " + name + "=");
		if (index != -1) {
			var s = cookieStr.substring(index + name.length + 3,
					cookieStr.length);
			return unescape(s.substring(0, s.indexOf("; ")))
		} else {
			return null
		}
	},
	set : function(name, value, expires) {
		var expDays = expires * 24 * 60 * 60 * 1000;
		var expDate = new Date();
		expDate.setTime(expDate.getTime() + expDays);
		var expString = expires ? "; expires=" + expDate.toGMTString() : "";
		var pathString = ";path=/";
		document.cookie = name + "=" + escape(value) + expString + pathString
	},
	del : function(name) {
		var exp = new Date(new Date().getTime() - 1);
		var s = this.get(name);
		if (s != null) {
			document.cookie = name + "=" + s + ";expires=" + exp.toGMTString()
					+ ";path=/"
		}
	}
};

function secBoard(elementID, listName, n) {
	var elem = document.getElementById(elementID);
	var elemlist = elem.getElementsByTagName("li");
	for (var i = 0; i < elemlist.length; i++) {
		elemlist[i].className = "normal";
		var m = i + 1;
		document.getElementById(listName + "_" + m).className = "normal";
	}
	elemlist[n - 1].className = "current";
	document.getElementById(listName + "_" + n).className = "current";

	$("#" + listName + "_" + n + ' img').each(function() {
		original($(this))
	});
}

function original(ob) {
	if (ob.attr('src').indexOf('loading') > -1 && ob.attr('original')) {
		ob.attr('src', ob.attr('original'));
	}
	return;
}

function cleartext(e) {
	e.value = '';
	e.focus();
}

function setInfo(id) {
	var con = document.getElementById('info_' + id);
	var btn = document.getElementById('info_btn_' + id);
	// alert( con.style.display);
	con.style.display = (con.style.display == 'none') ? "#" : "none";
	btn.style.display = (btn.style.display == 'none') ? "#" : "none";
}
$(function() {
	// 播放列表排序
	sortBtn();
});

function yx_tab(options) {
	var defaults = {
		tabs : "",
		box : "",
		evtype : "click",
		delay : 0,
		cur : "on",
		index : 0
	};
	var defaults = $.extend({}, defaults, options);
	if (defaults.evtype == 'mouseover' && defaults.delay > 0) {
		$(defaults.tabs).each(function(i) {
			$(this).hover(function() {
				if (YX_TABLE_TIME) {
					clearTimeout(YX_TABLE_TIME);
				}
				YX_TABLE_TIME = setTimeout(function() {
					yx_tab_do(defaults.tabs, defaults.box, defaults.cur, i);
				}, defaults.delay);
			}, function() {
				if (YX_TABLE_TIME) {
					clearTimeout(YX_TABLE_TIME);
				}
			});
		});
	} else {
		$(defaults.tabs).each(function(i) {
			$(this).bind(defaults.evtype, function() {
				yx_tab_do(defaults.tabs, defaults.box, defaults.cur, i);
			});
		});
	}
	yx_tab_do(defaults.tabs, defaults.box, defaults.cur, defaults.index);
}
function yx_tab_do(tabs, box, cur, i) {
	$(tabs).removeClass(cur).eq(i).addClass(cur);
	$(box).hide().eq(i).show();
}
